//
//  FtHomeViewController.m
//  Feast
//
//  Created by 胡光健 on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "FtHomeViewController.h"
#import "FtHomeBanner.h"
#import "FtHomeBtn.h"
#import "FtHomeBottom.h"
#import "ZJCellLayout.h"
#import "FtHomeCell.h"
#import "FtOrderManagementController.h"
#import "FtIncomeDetailsController.h"
#import "FtCommodityManagementController.h"
#import "FtActivityManagementController.h"

#define bannerH  335.0f
#define acBtnViewH  90.0f
#define marign     0.0f
#define taskDescViewW  (mScreenWidth - marign*2)*2/3
//左右边出头的itemW
#define insetMarign taskDescViewW/4
#define taskDescViewH  233.0f
#define taskScrollViewH  taskDescViewH

@interface FtHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)FtHomeBanner * bannerView;
@property(nonatomic,strong)FtHomeBtn * btnView;
@property(nonatomic,strong)FtHomeBottom * btmView;
@property(nonatomic,strong)UICollectionView * collectionView;

@end

@implementation FtHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rightNavImageName = @"Shape";
    [self configScrollView];
    [self configBanner];
    [self configCollectionView];
    [self configBtnView];
    [self configBottomView];
}


-(void)configScrollView {
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 0, mScreenWidth, mScreenHeight - 64 -44);
    _scrollView.contentSize = CGSizeMake(0, bannerH +acBtnViewH +taskDescViewH+12);
    _scrollView.backgroundColor = HJColorRGB(247);
    _scrollView.showsVerticalScrollIndicator = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
    _scrollView.mj_header = header;
    _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.view addSubview:_scrollView];
}
-(void)refreshDataWithHeader {
    
}


-(void)configBanner {
    WeakSelf
    self.bannerView = [[FtHomeBanner alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, 45)];
    self.bannerView.allOderBlock = ^(UIButton *btn) {
        [weakSelf pushOderManagement];
    };
    [self.scrollView addSubview:self.bannerView];
}
-(void)configCollectionView {
    ZJCellLayout *layout = [[ZJCellLayout alloc] init];
    layout.itemSize = CGSizeMake(taskDescViewW, 275);
    layout.minimumLineSpacing = marign;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.bannerView.frame), mScreenWidth, 285) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = NO;
    self.collectionView.clipsToBounds = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    if (iOS10after) {
        _collectionView.prefetchingEnabled = NO;
    }
    [self.collectionView registerNib:[UINib nibWithNibName:@"FtHomeCell" bundle:nil] forCellWithReuseIdentifier:@"FtHomeCell"];
    [self.scrollView addSubview:_collectionView];
}

-(void)configBtnView {
    WeakSelf
    self.btnView = [[FtHomeBtn alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.collectionView.frame), mScreenWidth, acBtnViewH)];
    self.btnView.btnBlock = ^(NSInteger tag) {
        if (tag==0) {// 订单管理
            [weakSelf pushOderManagement];
        }else if (tag==1) {//商品管理
            FtCommodityManagementController * CommodityVC= [[FtCommodityManagementController alloc]init];
            CommodityVC.navTitle = @"商品管理";
            [weakSelf.navigationController pushViewController:CommodityVC animated:YES];
        }else if (tag==2) {//收入明细
            FtIncomeDetailsController * IncomeVC = [[FtIncomeDetailsController alloc]init];
            IncomeVC.navTitle = @"收入明细";
            [weakSelf.navigationController pushViewController:IncomeVC animated:YES];
        }else if (tag==3) {//活动管理
            FtActivityManagementController * ActivityVC = [[FtActivityManagementController alloc]init];
            ActivityVC.navTitle = @"活动管理";
            [weakSelf.navigationController pushViewController:ActivityVC animated:YES];
        }
    };
    [self.scrollView addSubview:self.btnView];
}
-(void)pushOderManagement {
    FtOrderManagementController * OrderVC = [[FtOrderManagementController alloc]init];
    OrderVC.navTitle = @"订单管理";
    [self.navigationController pushViewController:OrderVC animated:YES];
}

-(void)configBottomView {
    self.btmView = [[FtHomeBottom alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.btnView.frame) + 10, mScreenWidth, 233)];
    [self.scrollView addSubview:self.btmView];
}

#pragma mark -- collectionViews数据源
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

//定义每个UICollectionView 的边距
- ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section {
    
    return UIEdgeInsetsMake ( 0, insetMarign+marign , 0 , insetMarign+marign );
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FtHomeCell" forIndexPath:indexPath];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark -- scrollViewDelegate

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (_collectionView == scrollView) {
        CGFloat x = _collectionView.contentOffset.x;
        CGFloat totalX = x + (taskDescViewW +marign)/2;
        CGFloat taskViewW = taskDescViewW;
        NSInteger index = totalX/taskViewW;
        CGFloat pointX = (taskViewW +marign) *index;
        [_collectionView setContentOffset:CGPointMake(pointX, 0) animated:YES];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (_collectionView == scrollView) {
        CGFloat x = _collectionView.contentOffset.x;
        CGFloat totalX = x + (taskDescViewW +marign)/2;
        CGFloat taskViewW = taskDescViewW;
        NSInteger index = totalX/taskViewW;
        CGFloat pointX = (taskViewW +marign) *index;
        [_collectionView setContentOffset:CGPointMake(pointX, 0) animated:YES];
    }
}

#pragma mark -- 消息
-(void)didClickRightNavAction {
    FtMessageViewController * vc = [[FtMessageViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
